<template>
  <el-dialog
    :title="'凭证详情'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    fullscreen
    center append-to-body>
    <el-form class="verify-form" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-row>
      <el-col :span="8"  v-if="dataForm.id != 0">
        <el-form-item label="单号" prop="billId" >
          <el-input v-model="dataForm.billId" placeholder="单号" readonly></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="凭证日期" prop="createTime">
          <el-input v-model="dataForm.createTime" placeholder="创建时间" readonly></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="凭证人" prop="createUserName">
          <el-input v-model="dataForm.createUserName" placeholder="创建用户名称" readonly></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="8">
        <el-form-item label="凭证类型" prop="type">
          <el-select v-model="dataForm.type" clearable @change="typeChange" disabled>
            <el-option label="付款单" :value="1"></el-option>
            <el-option label="收款单" :value="2"></el-option>
          </el-select>
          <el-select v-model="dataForm.twoType" disabled>
            <el-option label="付款单--发票" :value="1" v-if="dataForm.type == '1'"></el-option>
            <el-option label="付款单--单据" :value="2" v-if="dataForm.type == '1'"></el-option>
            <el-option label="预付款" :value="3" v-if="dataForm.type == '1'"></el-option>
            <el-option label="厂家减账" :value="4" v-if="dataForm.type == '1'"></el-option>
            <el-option label="其他付款" :value="5" v-if="dataForm.type == '1'"></el-option>
            <el-option label="调账" :value="6" v-if="dataForm.type == '1'"></el-option>
            <el-option label="客户批发" :value="7" v-if="dataForm.type == '2'"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="制造商" prop="typeName" v-if="dataForm.type == '1'">
          <el-input v-model="dataForm.typeName" readonly>
            <el-button slot="append" @click="checkSupplyList" disabled>选择</el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="客户名称" prop="typeName" v-if="dataForm.type == '2'">
          <el-input v-model="dataForm.typeName">
            <el-button slot="append" @click="checkCustomer" disabled>选择</el-button>
          </el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-form-item label="备注" prop="memo">
      <el-input v-model="dataForm.memo" placeholder="备注" type="textarea" readonly></el-input>
    </el-form-item>
    </el-form>

    <!-- <el-form :inline="true">
      <el-form-item>
        <el-button type="primary" @click="checkBill">选择单据</el-button>
      </el-form-item>
      <el-form-item style="float: right;">
        <el-button type="danger" @click="delTableData">删除</el-button>
      </el-form-item>
    </el-form> -->
    <el-table :data="dataList" border header-cell-class-name="miui-header-cell-style" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="40"></el-table-column>
      <el-table-column prop="linkBillId" label="单据号"></el-table-column>
      <el-table-column prop="createTime" label="制单日期"></el-table-column>
      <el-table-column prop="amount" label="单据金额"></el-table-column>
    </el-table>

    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <!-- <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
      <el-switch v-model="dataForm.status" active-text="保存并审核" inactive-value="0" active-value="1" style="margin-left: 15px;"></el-switch> -->
    </span>

    <!-- 弹窗, 制造商查询 -->
    <supply-search v-if="supplySearchVisible" ref="supplySearch" @refreshData="renewFormData"></supply-search>
    <!-- 弹窗, 选择单据 -->
    <check-bill v-if="payslipBillVisible" ref="payslipBill" @refreshData="billFormData"></check-bill>
    <!-- 弹窗, 选择客户 -->
    <check-customer v-if="customerVisible" ref="customer" @refreshData="customerData"></check-customer>
  </el-dialog>
</template>

<script>
    import {getObj, addObj, putObj} from '@/api/eims/evidence'
    import SupplySearch from "@/components/business/SearchSupply";
    import { mapGetters } from "vuex";
    import CheckBill from "./checkPayslip";
    import CheckCustomer from "./checkCustomer";
    export default {
    data () {
      return {
        visible: false,
        supplySearchVisible: false,
        payslipBillVisible: false,
        customerVisible: false,
        dataForm: {
          id: 0,
          templateId: '',
          billId: '',
          status: '0',
          evidenceType: '',
          billType: '',
          attachedNumber: '',
          relationId: '',
          relationName: '',
          memo: '',
          createTime: '',
          updateTime: '',
          createUser: '',
          modifyUser: '',
          orgId: '',
          tenantId: '',
          unitId: '',
          delFlag: '',
          typeName: '',
          twoType: ''
        },
        dataList: [],
        customerOptions:[],
        dataRule: {
          templateId: [
            { required: true, message: '凭证模板id不能为空', trigger: 'blur' }
          ],
          billId: [
            { required: true, message: '单号不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '审核状态(0-未审核,1-已审核)不能为空', trigger: 'blur' }
          ],
          evidenceType: [
            { required: true, message: '凭证类型(1-普通凭证,2-记账凭证)不能为空', trigger: 'blur' }
          ],
          billType: [
            { required: true, message: '单据种类', trigger: 'blur' }
          ],
          twoType: [
            { required: true, message: '凭证类型', trigger: 'blur' }
          ]
        }
      }
    },
    components: {
      SupplySearch,
      CheckBill,
      CheckCustomer
    },
    computed: {
      ...mapGetters(["userInfo"])
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getObj(this.dataForm.id).then(response => {
              if(response.data.code == '0'){
                this.dataForm = response.data.data
                this.dataList = response.data.data.detailList
              }else{
                this.$message.error(response.data.msg)
              }
            })
          }
        })
      },
      //显示当前时间（年月日时分秒）
      timeFormate(timeStamp) {
        let year = new Date(timeStamp).getFullYear();
        let month =
          new Date(timeStamp).getMonth() + 1 < 10 ?
          "0" + (new Date(timeStamp).getMonth() + 1) :
          new Date(timeStamp).getMonth() + 1;
        let date =
          new Date(timeStamp).getDate() < 10 ?
          "0" + new Date(timeStamp).getDate() :
          new Date(timeStamp).getDate();
        let hh =
          new Date(timeStamp).getHours() < 10 ?
          "0" + new Date(timeStamp).getHours() :
          new Date(timeStamp).getHours();
        let mm =
          new Date(timeStamp).getMinutes() < 10 ?
          "0" + new Date(timeStamp).getMinutes() :
          new Date(timeStamp).getMinutes();
        let ss =
          new Date(timeStamp).getSeconds() < 10 ?
          "0" + new Date(timeStamp).getSeconds() :
          new Date(timeStamp).getSeconds();
        this.nowTime =
          "POUT" +
          year +
          "" +
          month +
          "" +
          date +
          "" +
          hh +
          "" +
          mm +
          "" +
          ss +
          this.dataForm.deptId;
        this.dataForm.createTime = year + "-" + month + "-" + date;
      },
      // 表单提交
      dataFormSubmit () {
        if(this.dataList.length <=0){
          this.$message.warning("请选择单据")
          return;
        }
        if(this.dataForm.type == '1'){
          this.dataForm.billType = '1'
        }else{
          this.dataForm.billType = '3'
        }
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
                putObj(this.dataForm).then(response => {
                  if(response.data.code == '0'){
                    this.$message.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                  }else{
                    this.$message.error(response.data.msg)
                  }
                });
            } else {
                this.dataForm.createTime = ''
                addObj(this.dataForm).then(response => {
                  if(response.data.code == '0'){
                    this.$message.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                  }else{
                    this.$message.error(response.data.msg)
                  }
                })
            }
          }
        })
      },
      // 选择制造商
      checkSupplyList() {
        let data = {};
        this.supplySearchVisible = true;
        this.$nextTick(() => {
          this.$refs.supplySearch.init(data);
        });
      },
      renewFormData(data) {
        this.dataForm.supplyId = data.id;
        this.dataForm.typeName = data.name;
      },

      // 选择客户
      checkCustomer() {
        this.customerVisible = true;
        this.$nextTick(() => {
          this.$refs.customer.init();
        });
      },
      // 获取客户信息
      customerData(data) {
          this.dataForm.typeName = data.name
      },
      
      //凭证类型改变
      typeChange(){
        if(this.dataForm.type=='1'){
          this.dataForm.twoType ='1'
        }else if(this.dataForm.type=='2'){
          this.dataForm.twoType ='7'
        }else{
          this.dataForm.twoType = ''
        }
        this.dataForm.supplyId = ''
        this.dataForm.typeName = ''
      },
      handleSelectionChange(val) {
        this.selectionList = val;
      },
      // 删除选中的商品
      delTableData () {
        this.selectionList.forEach((item,index) => {
          for(let i = 0; i < this.dataList.length; i++){
            if(this.dataList[i] == item){
              this.dataList.splice(i,1)
              this.dataForm.detailList.splice(i,1)
            }
          }
          this.selectionList = []
        })
      },
      //选择单据
      checkBill(){
        if (!this.dataForm.type) {
          this.$message.warning("请选择凭证类型");
          return;
        }
        // 判断是否选中制造商
        if (!this.dataForm.supplyId && this.dataForm.type == '1') {
          this.$message.warning("请选择制造商");
          return;
        }
        if (!this.dataForm.typeName && this.dataForm.type == '2') {
          this.$message.warning("请选择客户");
          return;
        }
        let params = {
          supplyId: this.dataForm.supplyId,
          supplyName: this.dataForm.typeName,
        };
        this.payslipBillVisible = true
        this.$nextTick(() => {
          this.$refs.payslipBill.init(params)
        })
      },
      //更新表单数据
      billFormData(data) {
        console.log(data)
        this.dataList = JSON.parse(JSON.stringify(data));
        let list = [];
        data.forEach(item => {
          list.push({
            linkBillId: item.billId,
            createTime : item.createTime,
            amount: item.writeOffAmount,
            linkBillType: item.payType
          });
        });
        this.dataList = list;
        this.dataForm.detailList = list;
      },
    }
  }
</script>
